local skel = fk.CreateSkill {
  name = "emo__kuiye",
}

Fk:loadTranslationTable{
  ["emo__kuiye"] = "窥夜",
  [":emo__kuiye"] = "每当你受到伤害后，你可以进行一次判定，若判定牌点数之和不大于24，你可以重复判定，或将这些判定牌交给一名角色；若大于24，将这些牌置入弃牌堆。",

  ["#emo__kuiye-choose"] = "窥夜：你可以将这些牌交给一名角色；点“取消”：给自己",
  ["#emo__kuiye-invoke"] = "窥夜：你可重复判定，若不超过24点仍可获得牌（目前点数和：%arg），取消则立即获得牌",
}

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    local num = 0
    while player:isAlive() do
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|1~"..(24 - num),
        skipDrop = true, -- 判定后不进入弃牌堆
      }
      room:judge(judge)
      table.insert(cards, judge.card:getEffectiveId())
      num = num + judge.card.number
      if num > 24 or player.dead or
        not room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#emo__kuiye-invoke:::"..num}) then
        break
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if num <= 24 then
      local to = player
      local tos = room:askToChoosePlayers(player, { targets = room.alive_players,
      min_num = 1, max_num = 1, prompt = "#emo__kuiye-choose", skill_name = self.name })
      if #tos > 0 then
        to = tos[1]
      end
      room:obtainCard(to, cards, true, fk.ReasonGive, player, self.name)
    end
    room:cleanProcessingArea(cards, skel.name)
  end,
})

return skel
